package com.tygz.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.auto.base.controller.SysModuleController;
import com.auto.base.pojo.SysModule;
import com.auto.base.service.SysModuleService;
import com.base.common.RequestParameters;
import com.base.util.JUtil;
import com.tygz.service.SysRefreshService;


@Controller
@RequestMapping("/sys/SysModuleExt")
public class SysModuleExtController extends SysModuleController {
	private final Logger log = LoggerFactory.getLogger(SysModuleExtController.class);
	@Autowired
	private SysModuleService service;
	@Autowired
	private SysRefreshService sysRefreshService;
	
	
	@Override
	@RequestMapping(value = "addOrUpdate")
	@ResponseBody
	public Object addOrUpdate(@ModelAttribute SysModule record,HttpServletRequest request) {
		try {
			if(null == record){
				return setFail("接收数据为空！");
			}
			if(JUtil.isNULL(record.getId())) {
				record.setAddTime(new Date().getTime());
				record.setUpdateTime(record.getAddTime());
				this.service.insertSysModule(record);
				if(!JUtil.isNull(record.getModuleUrl())){
					//刷新拦截访问路径
					sysRefreshService.refreshSysAllowUrl();
				}
				return setSuccess("添加成功!");
			} 
			record.setUpdateTime(new Date().getTime());
			this.service.modifySysModuleByPkSelective(record);
			if(!JUtil.isNull(record.getModuleUrl())){
				//刷新拦截访问路径
				sysRefreshService.refreshSysAllowUrl();
			}
			return setSuccess("修改成功!");
		}catch(Exception e){
			log.error(e.getMessage(), e);
			return setFail("处理异常!");
		}
	}
	
	@Override
	@RequestMapping(value = "delete")
	@ResponseBody
	public Object delete(@ModelAttribute final  RequestParameters requestParameters) {
		try {
			if(StringUtils.isEmpty(requestParameters.getPks())){
				return setFail("删除ID为空!");
			}
			//刷新拦截访问路径
			sysRefreshService.refreshSysAllowUrl(requestParameters.getPks());
			return setSuccess("success");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return setException("fail",e.getMessage());
		}
	}
	
	/**
	 * 物理删除
	 * @return
	 */
	@RequestMapping(value = "drop")
	@ResponseBody
	public Object drop(@ModelAttribute final  RequestParameters requestParameters){
		try {
			if(StringUtils.isEmpty(requestParameters.getPks())){
				return setFail("删除ID为空!");
			}
			service.dropSysModule(requestParameters.getPks());
			
			//刷新拦截访问路径
			sysRefreshService.refreshSysAllowUrl(requestParameters.getPks());
			return setSuccess("success");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return setException("fail",e.getMessage());
		}
	}
	
}
